package cm.util;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static final String YYYY = "yyyy";
    public static final String YYYY_MM = "yyyy-MM";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private static final String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    public DateUtils() {
    }

    public static Date getNowDate() {
        return new Date();
    }

    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(String format, Date date) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static final Date dateTime(String format, String ts) {
        try {
            return (new SimpleDateFormat(format)).parse(ts);
        } catch (ParseException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        } else {
            try {
                return parseDate(str.toString(), parsePatterns);
            } catch (ParseException var2) {
                return null;
            }
        }
    }

    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    public static String timeDistance(Date endDate, Date startTime) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long diff = endDate.getTime() - startTime.getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static Date toDate(LocalDateTime temporalAccessor) {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    public static Date toDate(LocalDate temporalAccessor) {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     获取任意一天的开始时间
     amount = -1  表示昨天
     amount = 0   表示今天
     amount = 1   表示明天
     */
    public static Date getEveryDayStart(int amount) {
        Date beginOfDay = DateUtil.beginOfDay(new Date());
        Calendar c = Calendar.getInstance();
        c.setTime(beginOfDay);
        c.add(Calendar.DAY_OF_MONTH, amount);
        Date startDate = c.getTime();
        return startDate;
    }

    /**
     获取任意一天的结束时间
     amount = -1  表示昨天
     amount = 0   表示今天
     amount = 1   表示明天
     */
    public static Date getEveryDayEnd(int amount) {
        Calendar c = Calendar.getInstance();
        Date endOfDay = DateUtil.endOfDay(new Date());
        c.setTime(endOfDay);
        c.add(Calendar.DAY_OF_MONTH, amount);
        Date endDate = c.getTime();
        return endDate;
    }

    /**
     * 以当天为中心,具体用法如下
     * day<0 向后获取过去的某一天（例如昨天）
     * day=0 获取当天 的日期
     * dayx>0 向前获取未来的某一天（例如后天）
     * 获取某天最早时刻，一般情况我们用到的最多的是获取当天的最早时刻，即参数day=0
     * return 日期
     */
    public static String getDayStartDate(int day, String format) {
        return LocalDateTime.of(LocalDate.now().plusDays(day), LocalTime.MIN).
                format(DateTimeFormatter.ofPattern(StringUtils.isNotEmpty(format) ? format : YYYY_MM_DD_HH_MM_SS));
    }

    /**
     * 以当天为中心,具体用法如下
     * day<0 向后获取过去的某一天（例如昨天）
     * day=0 获取当天 的日期
     * dayx>0 向前获取未来的某一天（例如后天）
     * 获取某天最晚时刻，一般情况我们用到的最多的是获取当天的最晚时刻，即参数day=0
     * return 日期
     */
    public static String getDayEndDate(int day, String format) {
        return LocalDateTime.of(LocalDate.now().plusDays(day), LocalTime.MAX).
                format(DateTimeFormatter.ofPattern(StringUtils.isNotEmpty(format) ? format : YYYY_MM_DD_HH_MM_SS));
    }

    public static LocalDateTime getLocalDateTime(String currentDate, String format) {
        DateTimeFormatter dfr = DateTimeFormatter.ofPattern(StringUtils.isNotEmpty(format) ? format : YYYY_MM_DD_HH_MM_SS);
        LocalDateTime dateTime = LocalDateTime.parse(currentDate, dfr);
        return dateTime;
    }
}
